package com.yxy.controller.user;

import com.yxy.common.PageResult;
import com.yxy.common.Result;
import com.yxy.constant.MessageConstant;
import com.yxy.constant.RequestMapperConstant;
import com.yxy.exception.QRCodeGeneratorException;
import com.yxy.pojo.dto.HotelBookingDTO;
import com.yxy.pojo.dto.HotelBookingPageQueryDTO;
import com.yxy.pojo.vo.HotelBookingVo;
import com.yxy.pojo.vo.SightsBookingVO;
import com.yxy.service.HotelBookingService;
import com.yxy.utils.QRCodeGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@RestController("userHotelBookingController")
@RequestMapping(RequestMapperConstant.USER_REQUEST_MAPPER + "hotelBooking")
@Slf4j
public class HotelBookingController {

    @Autowired
    private HotelBookingService hotelBookingService;

    // 对应前端页面跳转地址
    public static final String URL = "http://tourism.ymvpear.icu/#/hotel/booking/qr/";

    /**
     * 添加酒店预定信息
     * @param hotelBookingDTO
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody HotelBookingDTO hotelBookingDTO){
        log.info("添加酒店预定信息：{}", hotelBookingDTO);
        hotelBookingService.add(hotelBookingDTO);
        return Result.success("已预约");
    }

    /**
     * 分页查询用户酒店预约
     * @param hotelBookingPageQueryDTO
     * @return
     */
    @GetMapping("/getHotelBookingList")
    public Result<PageResult> pageQueryHotelBooking(HotelBookingPageQueryDTO hotelBookingPageQueryDTO){
        log.info("分夜查询用户酒店预约：{}", hotelBookingPageQueryDTO);
        PageResult pageResult = hotelBookingService.pageQueryHotelBooking(hotelBookingPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 获取用户酒店预定信息
     * @param id
     * @return
     */
    @GetMapping("/getHotelBookingById")
    public Result<HotelBookingVo> getSightsBookingById(Integer id){
        log.info("获取用户酒店预定信息：{}", id);
        HotelBookingVo vo = hotelBookingService.getHotelBookingById(id);
        return Result.success("", vo);
    }

    /**
     * 取消酒店预定信息
     * @param id
     * @return
     */
    @PutMapping("/cancelHotelBooking/{bookingId}/{hotelId}/{hotelSpecsId}/{userId}")
    public Result cancelSightsBooking(@PathVariable Integer hotelId, @PathVariable Integer hotelSpecsId, @PathVariable Integer userId, @PathVariable Integer bookingId) {
        log.info("取消用户酒店预定信息：酒店id,{}. 酒店规格id：{}，用户id：{}, 预约id，{}", hotelId, hotelSpecsId, userId, bookingId);
        hotelBookingService.cancelHotelBooking(hotelId, hotelSpecsId, userId, bookingId);
        return Result.success("取消成功");
    }

    /**
     * 使用酒店预定信息
     * @param id
     * @return
     */
    @PutMapping("/useHotelBooking/{id}")
    public Result<String> use(@PathVariable int id){
        log.info("使用酒店预定信息：{}", id);
        String status = hotelBookingService.use(id);
        return Result.success("", status);
    }

    /**
     * 生成二维码
     * @param id
     * @return
     */
    @GetMapping("/getQRCode")
    public Result QRCode(Integer id){
        log.info("生成景点预约二维码：{}", id);
        String base64 = "";
        try {
            String path = QRCodeGenerator.generateQRCodeImage(URL + id, 100, 100, id, QRCodeGenerator.HOTEL);
            // 转成Base64
            byte[] imageBytes = Files.readAllBytes(Paths.get(path));
            base64 = Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            throw new QRCodeGeneratorException(MessageConstant.QR_CODE_GENERATION_FAILURE);
        }
        return Result.success("", base64);
    }
}
